

package com.loong.loong.hik.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loong.loong.common.core.util.R;
import com.loong.loong.common.excel.annotation.ResponseExcel;
import com.loong.loong.common.log.annotation.SysLog;
import com.loong.loong.hik.entity.HikCdcMessagePublish;
import com.loong.loong.hik.service.HikCdcMessagePublishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 信息引导及发布
 *
 * @author jinyiang
 * @date 2023-04-26 10:28:06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/hikcdcmessagepublish" )
@Api(value = "hikcdcmessagepublish", tags = "信息引导及发布管理")
public class HikCdcMessagePublishController {

    private final  HikCdcMessagePublishService hikCdcMessagePublishService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param hikCdcMessagePublish 信息引导及发布
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('hik_hikcdcmessagepublish_view')" )
    public R getHikCdcMessagePublishPage(Page page, HikCdcMessagePublish hikCdcMessagePublish) {
		return R.ok(hikCdcMessagePublishService.page(page,
				Wrappers.query(hikCdcMessagePublish).lambda()
						.like(StringUtils.isNotEmpty(hikCdcMessagePublish.getLikeName()), HikCdcMessagePublish::getContent,hikCdcMessagePublish.getLikeName())
						.between(StringUtils.isNotEmpty(hikCdcMessagePublish.getStartTime())&&StringUtils.isNotEmpty(hikCdcMessagePublish.getEndTime()),HikCdcMessagePublish::getCreateTime,hikCdcMessagePublish.getStartTime(),hikCdcMessagePublish.getEndTime())
				.between(StringUtils.isNotEmpty(hikCdcMessagePublish.getStartTime2())&&StringUtils.isNotEmpty(hikCdcMessagePublish.getEndTime2()),HikCdcMessagePublish::getPublishTime,hikCdcMessagePublish.getStartTime2(),hikCdcMessagePublish.getEndTime2()))
		);

    }


    /**
     * 通过id查询信息引导及发布
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('hik_hikcdcmessagepublish_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(hikCdcMessagePublishService.getById(id));
    }

    /**
     * 新增信息引导及发布
     * @param hikCdcMessagePublish 信息引导及发布
     * @return R
     */
    @ApiOperation(value = "新增信息引导及发布", notes = "新增信息引导及发布")
    @SysLog("新增信息引导及发布" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('hik_hikcdcmessagepublish_add')" )
    public R save(@RequestBody HikCdcMessagePublish hikCdcMessagePublish) {
		HikCdcMessagePublish hikCdcMessagePublish1 = new HikCdcMessagePublish();
		hikCdcMessagePublish1.setIndexCode(hikCdcMessagePublish.getIndexCode());
		long count = hikCdcMessagePublishService.count(Wrappers.query(hikCdcMessagePublish1));
		if (count>0){
			return R.failed("已存在相同的编号");
		}
        return R.ok(hikCdcMessagePublishService.save(hikCdcMessagePublish));
    }

    /**
     * 修改信息引导及发布
     * @param hikCdcMessagePublish 信息引导及发布
     * @return R
     */
    @ApiOperation(value = "修改信息引导及发布", notes = "修改信息引导及发布")
    @SysLog("修改信息引导及发布" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('hik_hikcdcmessagepublish_edit')" )
    public R updateById(@RequestBody HikCdcMessagePublish hikCdcMessagePublish) {
		HikCdcMessagePublish hikCdcMessagePublish1 = new HikCdcMessagePublish();
		hikCdcMessagePublish1.setId(hikCdcMessagePublish.getId());
		HikCdcMessagePublish one = hikCdcMessagePublishService.getOne(Wrappers.query(hikCdcMessagePublish1));
		if (!one.getIndexCode().equals(hikCdcMessagePublish.getIndexCode())){
			HikCdcMessagePublish hikCdcMessagePublish2 = new HikCdcMessagePublish();
			hikCdcMessagePublish2.setIndexCode(hikCdcMessagePublish.getIndexCode());
			long count = hikCdcMessagePublishService.count(Wrappers.query(hikCdcMessagePublish2));
			if (count>0){
				return R.failed("已存在相同的编号");
			}
		}
        return R.ok(hikCdcMessagePublishService.updateById(hikCdcMessagePublish));
    }

    /**
     * 通过id删除信息引导及发布
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除信息引导及发布", notes = "通过id删除信息引导及发布")
    @SysLog("通过id删除信息引导及发布" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('hik_hikcdcmessagepublish_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(hikCdcMessagePublishService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param hikCdcMessagePublish 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('hik_hikcdcmessagepublish_export')" )
    public List<HikCdcMessagePublish> export(HikCdcMessagePublish hikCdcMessagePublish) {
        return hikCdcMessagePublishService.list(Wrappers.query(hikCdcMessagePublish));
    }
}
